<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<bean:define id="nbEntitesSel" name="PlanningForm" property="nbEntitesSel" />
<bean:define id="nbSousCases" value="${(nbEntitesSel > 0) ? nbEntitesSel : 1}" />

<%@page import="gestionedt.models.Creneau" %>
<% pageContext.setAttribute("nbJours", Creneau.getNbJours()); %>
<% pageContext.setAttribute("nbPlagesParJour", Creneau.getNbCreneauxParJour()); %>

<c:if test="${SelectionJourForm.zoomJour}">
    <bean:define id="numJourDebut" value="${SelectionJourForm.numJourSel}" />
    <bean:define id="numJourFin" value="${SelectionJourForm.numJourSel}" />
</c:if>
<c:if test="${ ! SelectionJourForm.zoomJour}">
    <bean:define id="numJourDebut" value="1" />
    <bean:define id="numJourFin" value="${nbJours}" />
</c:if>

<table class="edt">
    <thead>
        <tr>
            <th class="coinGauche" />
            <c:forEach begin="${numJourDebut}" end="${numJourFin}" var="i">
                <th colspan="${nbSousCases}" class="jour">
                  <html:link
                      action="/SelectionnerJour"
                      paramId="numJourSel" paramName="i"
                      title="${numJourDebut==numJourFin ? 'Afficher les autres jours' : 'Afficher uniquement ce jour'}">
                    <%= Creneau.getJour((Integer)pageContext.getAttribute("i")) %>
                  </html:link>
                </th>
            </c:forEach>
        </tr>
        <c:if test="${nbEntitesSel > 0}">
            <tr>
                <th class="coinGauche" />

                <bean:define id="nbJoursAffiches" value="${SelectionJourForm.zoomJour ? 1 : nbJours}" />
                <bean:define id="styleLargeur" value="style=\"width: ${100/nbEntitesSel/nbJoursAffiches}%;\"" />
                <bean:define id="nbGroupesSel" name="PlanningForm" property="nbGroupesSel" />
                <bean:define id="nbEnseignantsSel" name="PlanningForm" property="nbEnseignantsSel" />

                <c:forEach begin="${numJourDebut}" end="${numJourFin}">
                    <logic:iterate id="grpSel" name="PlanningForm" property="groupesSel" indexId="i">
                        <bean:define id="nEstPasDerniere" value="(i==nbGroupesSel-1 && nbEnseignantsSel==0)" />
                        <th class="entite" ${nEstPasDerniere ? "" : styleLargeur}>
                            ${grpSel.libelle}
                        </th>
                    </logic:iterate>
                    <logic:iterate id="ensSel" name="PlanningForm" property="enseignantsSel" indexId="i">
                        <bean:define id="nEstPasDerniere" value="(i == nbEnseignantsSel-1)" />
                        <th class="entite" ${nEstPasDerniere ? "" : styleLargeur}>
                            ${ensSel.identite}
                        </th>
                    </logic:iterate>
                </c:forEach>
            </tr>
        </c:if>
    </thead>
    <tbody>
        <bean:define id="heureDebut" value="8" />
        <c:forEach var="j" begin="1" end="${nbPlagesParJour}">
            <c:if test="${j == 3}">
                <tr class="repas">
                    <th class="horaire">${heureDebut}h</th>
                    <td colspan="${nbJours * nbSousCases}" />
                </tr>
                <bean:define id="heureDebut" value="${heureDebut + 2}" />
            </c:if>
            <tr class="ligneCreneaux">
                <th class="horaire">${heureDebut}h</th>
                <c:forEach var="i" begin="${numJourDebut}" end="${numJourFin}">

                    <c:if test="${nbEntitesSel > 0}">
                        <bean:define id="numCreneau" value="${(j+((i-1)*nbPlagesParJour))}" />
                        <bean:define id="nbSsCasesFus"
                                     name="PlanningForm"
                                     property="planning.case[${numCreneau}].nbSousCasesFusionnees" />
                        <bean:define id="k" value="0" />
                        
                        <logic:iterate id="sousCase"
                                       name="PlanningForm"
                                       property="planning.case[${numCreneau}].sousCases">

                            <bean:define id="estPremiere" value="${(k == 0)}" />
                            <bean:define id="estDerniere" value="${(k == nbSsCasesFus-1)}" />

                            <c:if test="${sousCase.nbColonnes > 0}">

                                <c:if test="${sousCase.prestation != null}">

                                    <td colspan="${sousCase.nbColonnes}"
                                        class="case ${estPremiere?"premiere":""} ${estDerniere?"derniere":""}"
                                        style="background-color: ${sousCase.prestation.module.bgColor};
                                               color: ${sousCase.prestation.module.fontColor};">

                                      <div class="case occupee ${sousCase.className}">

                                        <span class="numCreneau cache" style="display: none;">${numCreneau}</span>

                                        <div class="prestation casee ${PlanningForm.classNamesPrest[sousCase.prestation.id]}">

                                            <span class="idPrest" style="display: none;">
                                                ${sousCase.prestation.id}
                                            </span>
                                            <div class="module" title="UE : ${sousCase.prestation.module.ue}">
                                                ${sousCase.prestation.module.libelle}
                                            </div>
                                            <div class="enseignant">
                                                ${sousCase.prestation.enseignant.identite}
                                            </div>

                                        </div>

                                      </div>

                                    </td>

                                </c:if>

                                <c:if test="${sousCase.prestation == null}">
                                    <td class="case ${estPremiere?"premiere":""} ${estDerniere?"derniere":""}"
                                        style="background-color: white;">
                                        <div class="case ${sousCase.className}">
                                            <span class="numCreneau cache" style="display: none;">${numCreneau}</span>
                                        </div>
                                    </td>
                                </c:if>

                                <bean:define id="k" value="${k+1}" />
                            </c:if>

                        </logic:iterate>

                    </c:if>

                    <c:if test="${nbEntitesSel == 0}">
                        <td class="case premiere derniere" style="background-color: white;">
                            <div class="case" />
                        </td>
                    </c:if>

                </c:forEach>
            </tr>
            <bean:define id="heureDebut" value="${heureDebut + 2}" />
        </c:forEach>

        <tr class="derniereLigne">
            <th class="horaire fin">${heureDebut}h</th>
            <td colspan="${nbJours * nbSousCases}" />
        </tr>
        
    </tbody>
</table>